package com.uestc.controller;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uestc.entity.Host;
import com.uestc.entity.NewsTypeTemplate;
import com.uestc.entity.ResearchPlatform;
import com.uestc.mapper.AboutUsDetailMapper;
import com.uestc.mapper.AboutUsMapper;
import com.uestc.mapper.ContactUsMapper;
import com.uestc.mapper.LinkMapper;
import com.uestc.mapper.NewsCategoryMapper;
import com.uestc.mapper.NewsMapper;
import com.uestc.mapper.PartMapper;
import com.uestc.mapper.RelatedLinkMapper;
import com.uestc.mapper.ServicesMapper;
import com.uestc.mapper.SliderMapper;
import com.uestc.mapper.Training1Mapper;
import com.uestc.mapper.TrainingLinkMapper;
import com.uestc.mapper.TrainingListMapper;
import com.uestc.mapper.TrainingMapper;
import com.uestc.mapper.UserMapper;
import com.uestc.pojo.AboutUs;
import com.uestc.pojo.AboutUsDetail;
import com.uestc.pojo.AboutUsDetailExample;
import com.uestc.pojo.ContactUs;
import com.uestc.pojo.ContactUsExample;
import com.uestc.pojo.Link;
import com.uestc.pojo.LinkExample;
import com.uestc.pojo.News;
import com.uestc.pojo.NewsCategoryExample;
import com.uestc.pojo.NewsExample;
import com.uestc.pojo.NewsExample.Criteria;
import com.uestc.repository.ResearchPlatformCategoryRepository;
import com.uestc.repository.ResearchPlatformRepository;
import com.uestc.pojo.Part;
import com.uestc.pojo.PartExample;
import com.uestc.pojo.RelatedLink;
import com.uestc.pojo.RelatedLinkExample;
import com.uestc.pojo.Services;
import com.uestc.pojo.Slider;
import com.uestc.pojo.SliderExample;
import com.uestc.pojo.Training;
import com.uestc.pojo.Training1;
import com.uestc.pojo.TrainingLink;
import com.uestc.pojo.TrainingList;
import com.uestc.pojo.TrainingListExample;
import com.uestc.pojo.User;
import com.uestc.pojo.UserExample;
import com.uestc.util.HttpUtil;
import com.uestc.util.date.DateStyle;
import com.uestc.util.date.DateUtil;

@Controller
@RequestMapping(value = "/admin")
public class AdminPageController {
	private static String PATH = "admin/";
	@Autowired
	private NewsMapper newsMapper;
	@Autowired
	private NewsCategoryMapper categoryMapper;
	@Autowired
	private AboutUsMapper aboutUsMapper;
	@Autowired
	private AboutUsDetailMapper aboutUsDetailMapper;
	@Autowired
	private SliderMapper sliderMapper;
	@Autowired
	private ServicesMapper servicesMapper;
	@Autowired
	private ContactUsMapper contactUsMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private LinkMapper linkMapper;
	@Autowired
	private PartMapper partMapper;
	@Autowired
	private RelatedLinkMapper relatedLinkMapper;
	@Autowired
	private Host host;
	@Autowired
	private TrainingMapper trainingMapper;
	@Autowired
	private Training1Mapper training1Mapper;
	@Autowired
	private TrainingListMapper trainingListMapper;
	@Autowired
	private TrainingLinkMapper trainingLinkMapper;
	@Autowired
	private ResearchPlatformRepository researchPlatformRepository;
	@Autowired
	private ResearchPlatformCategoryRepository researchPlatformCategoryRepository;

	@GetMapping(value = { "", "/", "/index" })
	public String index(Model model) {
		return "redirect:/admin/aboutus/0";
	}

	@GetMapping(value = { "/news/{id}", })
	public String news(@PathVariable Long id, Model model,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		model.addAttribute("categoryInfo", categoryMapper.selectByPrimaryKey(id));
		NewsCategoryExample example1 = new NewsCategoryExample();
		model.addAttribute("newsCategories", categoryMapper.selectByExample(example1));
		PageHelper.startPage(pageNum, 10);
		NewsExample example2 = new NewsExample();
		example2.setOrderByClause("create_time desc");
		Criteria c2 = example2.createCriteria();
		c2.andCategoryIdEqualTo(id);
		List<News> list = newsMapper.selectByExample(example2);
		PageInfo<News> pageInfo = new PageInfo<>(list);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("news", list);
		return PATH + "pages/news";
	}

	@GetMapping(value = { "/addnews", })
	public String addnews(Model model) {
		NewsCategoryExample example1 = new NewsCategoryExample();
		model.addAttribute("newsCategories", categoryMapper.selectByExample(example1));
		return PATH + "pages/addnews";
	}

	@GetMapping(value = "/delNew/{categoryId}/{id}")
	public String delNew(Model model, @PathVariable(required = true) Integer categoryId,
			@PathVariable(required = true) String id) {
		newsMapper.deleteByPrimaryKey(id);
		return "redirect:/admin/news/" + categoryId;
	}

	@PostMapping(value = "/addNews")
	@Transactional
	public String addNews(NewsTypeTemplate newsTypeTemplate, MultipartFile img, MultipartFile file) {
		// 时间转换 --start
		News news = new News();
		BeanUtils.copyProperties(newsTypeTemplate, news);
		news.setCreateTime(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		// 时间转换 --end
		String focusPicture = HttpUtil.saveImage(img, host);
		news.setFocusPicture(focusPicture);
		news.setId(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_ME) + new Random().nextInt(10));
		news.setCategoryName(categoryMapper.selectByPrimaryKey(news.getCategoryId()).getCategoryName());
		newsMapper.insertSelective(news);
		if (file != null && !file.isEmpty()) {
			String fileUrl = HttpUtil.saveFile(file, host);
			Link record = new Link();
			record.setParentId(news.getId());
			// 加图标样式
			if (fileUrl.toLowerCase().contains(".zip") || fileUrl.toLowerCase().contains(".rar")) {
				record.setIconClass("fa fa-file-zip-o");
			} else if (fileUrl.toLowerCase().contains(".gif") || fileUrl.toLowerCase().contains(".jpg")
					|| fileUrl.toLowerCase().contains(".png")) {
				record.setIconClass("fa fa-file-image-o");
			} else if (fileUrl.toLowerCase().contains(".wav") || fileUrl.toLowerCase().contains(".mp3")) {
				record.setIconClass("fa fa-file-audio-o");
			} else if (fileUrl.toLowerCase().contains(".avi") || fileUrl.toLowerCase().contains(".mpg")) {
				record.setIconClass("fa fa-file-video-o");
			} else if (fileUrl.toLowerCase().contains(".doc") || fileUrl.toLowerCase().contains(".docx")) {
				record.setIconClass("fa fa-file-word-o");
			} else if (fileUrl.toLowerCase().contains(".ppt") || fileUrl.toLowerCase().contains(".pptx")) {
				record.setIconClass("fa fa-file-powerpoint-o");
			} else if (fileUrl.toLowerCase().contains(".pdf")) {
				record.setIconClass("fa fa-file-pdf-o");
			} else if (fileUrl.toLowerCase().contains(".xls") || fileUrl.toLowerCase().contains(".xlsx")) {
				record.setIconClass("fa fa-file-excel-o");
			} else {
				record.setIconClass("fa fa-folder-o");
			}
			record.setUrl(fileUrl);
			linkMapper.insertSelective(record);
		}
		return "redirect:/admin/addnews";
	}

	@GetMapping(value = { "/updatenew/{id}" })
	public String updatenews(Model model, @PathVariable String id) {
		model.addAttribute("news", newsMapper.selectByPrimaryKey(id));
		return PATH + "pages/updatenew";
	}

	@PostMapping(value = { "/updateNew/{id}" })
	public String updateNews(Model model, @PathVariable String id, NewsTypeTemplate newsTypeTemplate, MultipartFile img,
			MultipartFile file) {
		// 时间转换 --start
		News news = new News();
		BeanUtils.copyProperties(newsTypeTemplate, news);
		news.setCreateTime(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		// 时间转换 --end
		String focusPicture = HttpUtil.saveImage(img, host);
		news.setFocusPicture(focusPicture);
		news.setId(id);
		newsMapper.updateByPrimaryKeySelective(news);
		if (file != null && !file.isEmpty()) {
			LinkExample example = new LinkExample();
			com.uestc.pojo.LinkExample.Criteria c = example.createCriteria();
			c.andParentIdEqualTo(id);
			linkMapper.deleteByExample(example);
			String fileUrl = HttpUtil.saveFile(file, host);
			Link record = new Link();
			record.setParentId(news.getId());
			// 加图标样式
			if (fileUrl.toLowerCase().contains(".zip") || fileUrl.toLowerCase().contains(".rar")) {
				record.setIconClass("fa fa-file-zip-o");
			} else if (fileUrl.toLowerCase().contains(".gif") || fileUrl.toLowerCase().contains(".jpg")
					|| fileUrl.toLowerCase().contains(".png")) {
				record.setIconClass("fa fa-file-image-o");
			} else if (fileUrl.toLowerCase().contains(".wav") || fileUrl.toLowerCase().contains(".mp3")) {
				record.setIconClass("fa fa-file-audio-o");
			} else if (fileUrl.toLowerCase().contains(".avi") || fileUrl.toLowerCase().contains(".mpg")) {
				record.setIconClass("fa fa-file-video-o");
			} else if (fileUrl.toLowerCase().contains(".doc") || fileUrl.toLowerCase().contains(".docx")) {
				record.setIconClass("fa fa-file-word-o");
			} else if (fileUrl.toLowerCase().contains(".ppt") || fileUrl.toLowerCase().contains(".pptx")) {
				record.setIconClass("fa fa-file-powerpoint-o");
			} else if (fileUrl.toLowerCase().contains(".pdf")) {
				record.setIconClass("fa fa-file-pdf-o");
			} else if (fileUrl.toLowerCase().contains(".xls") || fileUrl.toLowerCase().contains(".xlsx")) {
				record.setIconClass("fa fa-file-excel-o");
			} else {
				record.setIconClass("fa fa-folder-o");
			}
			record.setUrl(fileUrl);
			linkMapper.insertSelective(record);
		}
		return "redirect:/admin/updatenew/" + id;
	}

	@GetMapping(value = { "/contactus" })
	public String contactus(Model model) {
		ContactUsExample e1 = new ContactUsExample();
		model.addAttribute("contactUs", contactUsMapper.selectByExampleWithBLOBs(e1).get(0));
		return PATH + "pages/contactus";
	}

	@PostMapping(value = "/saveContactUs")
	public String saveContactUs(Model model, ContactUs record) {
		ContactUsExample example = new ContactUsExample();
		contactUsMapper.updateByExampleSelective(record, example);
		return "redirect:/admin/contactus";
	}

	@GetMapping(value = { "/party" })
	public String party(Model model, @RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		PageHelper.startPage(pageNum, 10);
		PartExample example = new PartExample();
		example.setOrderByClause("create_time desc");
		List<Part> list = partMapper.selectByExample(example);
		PageInfo<Part> pageInfo = new PageInfo<>(list);
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("partys", list);
		return PATH + "pages/party";
	}

	@GetMapping(value = { "/addparty" })
	public String addparty(Model model) {
		return PATH + "pages/addparty";
	}

	@GetMapping(value = { "/delParty/{id}" })
	public String delParty(Model model, @PathVariable Long id) {
		partMapper.deleteByPrimaryKey(id);
		return "redirect:/admin/party";
	}

	@PostMapping(value = "/addParty")
	public String addParty(NewsTypeTemplate newsTypeTemplate) {
		Part part = new Part();
		BeanUtils.copyProperties(newsTypeTemplate, part);
		part.setCreateTime(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		System.out.println(part.getCreateTime());
		partMapper.insertSelective(part);
		return "redirect:/admin/addparty";
	}

	@GetMapping(value = { "/updateparty/{id}" })
	public String updateparty(Model model, @PathVariable Long id) {
		model.addAttribute("party", partMapper.selectByPrimaryKey(id));
		return PATH + "pages/updateparty";
	}

	@PostMapping(value = { "/updateParty/{id}" })
	public String updateParty(Model model, @PathVariable Long id, NewsTypeTemplate newsTypeTemplate) {
		Part part = new Part();
		BeanUtils.copyProperties(newsTypeTemplate, part);
		part.setCreateTime(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		part.setId(id);
		partMapper.updateByPrimaryKeySelective(part);
		return "redirect:/admin/updateparty/" + id;
	}

	/**
	 * 成研介绍
	 */
	@GetMapping(value = { "/aboutus/{id}" })
	public String aboutus(Model model, @PathVariable(required = true) Integer id,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		if (id == 1) {
			PageHelper.startPage(pageNum, 10);
			AboutUsDetailExample example = new AboutUsDetailExample();
			example.setOrderByClause("create_time desc");
			List<AboutUsDetail> list = aboutUsDetailMapper.selectByExample(example);
			PageInfo<AboutUsDetail> pageInfo = new PageInfo<>(list);
			model.addAttribute("pageInfo", pageInfo);
			model.addAttribute("aboutUsDetails", list);
			return PATH + "pages/aboutus2";
		} else {
			model.addAttribute("aboutUsInfo", aboutUsMapper.selectByPrimaryKey(id));
			return PATH + "index";
		}
	}

	@PostMapping(value = "/saveAboutUs/{id}")
	public String saveAboutUs1(Model model, AboutUs record, @PathVariable(required = true) Integer id) {
		record.setId(id);
		aboutUsMapper.updateByPrimaryKeySelective(record);
		return "redirect:/admin/aboutus/" + id;
	}

	@PostMapping(value = "/saveAboutUs2")
	public String saveAboutUs2(Model model, AboutUsDetail record) {
		record.setCategoryId(2);
		aboutUsDetailMapper.insertSelective(record);
		return "redirect:/admin/aboutus/1";
	}

	@PostMapping(value = "/updateAboutUs2/{id}")
	public String updateAboutUs2(Model model, @PathVariable Long id, AboutUsDetail record) {
		record.setId(id);
		aboutUsDetailMapper.updateByPrimaryKeySelective(record);
		return "redirect:/admin/aboutus/1";
	}

	@GetMapping(value = { "/delAboutUsDetail/{id}" })
	public String delAboutUsDetail(Model model, @PathVariable Long id) {
		aboutUsDetailMapper.deleteByPrimaryKey(id);
		return "redirect:/admin/aboutus/1";
	}

	@GetMapping(value = { "/slider" })
	public String slide(Model model) {
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(0);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		SliderExample example2 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c2 = example2.createCriteria();
		c2.andTypeEqualTo(1);
		model.addAttribute("sliderList2", sliderMapper.selectByExample(example2));
		return PATH + "pages/slider";
	}

	@PostMapping(value = "/updateSlider")
	public String updateSlider(Model model, MultipartFile file, Slider record) {
		String path = HttpUtil.saveImage(file, host);
		record.setPictureUrl(path);
		sliderMapper.updateByPrimaryKey(record);
		return "redirect:/admin/slider";
	}

	@PostMapping(value = "/addSlider")
	public String addSlider(Model model, MultipartFile file, Integer type) {
		saveImage(file, type);
		return "redirect:/admin/slider";
	}

	private void saveImage(MultipartFile file, Integer type) {
		String path = HttpUtil.saveImage(file, host);
		Slider record = new Slider();
		record.setType(type);
		record.setPictureUrl(path);
		sliderMapper.insertSelective(record);
	}

	@GetMapping(value = "/delSlider/{id}")
	public String delSlider(Model model, @PathVariable(required = true) Integer id) {
		sliderMapper.deleteByPrimaryKey(id);
		return "redirect:/admin/slider";
	}

	@GetMapping(value = "/services/{id}")
	public String services(Model model, @PathVariable(required = true) Integer id) {
		model.addAttribute("serviceInfo", servicesMapper.selectByPrimaryKey(id));
		return PATH + "pages/services";
	}

	@PostMapping(value = "/saveService/{id}")
	public String saveService(Model model, @PathVariable(required = true) Integer id, Services record) {
		record.setId(id);
		servicesMapper.updateByPrimaryKeySelective(record);
		return "redirect:/admin/services/" + id;
	}

	@GetMapping(value = "/training/{id}")
	public String training(Model model, @PathVariable(required = true) Integer id) {
		model.addAttribute("trainingInfo", trainingMapper.selectByPrimaryKey(id));
		return PATH + "pages/training";
	}

	@PostMapping(value = "/saveTraining/{id}")
	public String saveTraining(Model model, @PathVariable(required = true) Integer id, Training record) {
		record.setId(id);
		trainingMapper.updateByPrimaryKeySelective(record);
		return "redirect:/admin/training/" + id;
	}

	@GetMapping(value = "/training1/{id}")
	public String training1(Model model, @PathVariable(required = true) Integer id,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		if (id == 1) {
			model.addAttribute("trainingInfo", training1Mapper.selectByPrimaryKey(id));
			return PATH + "pages/training1";
		} else {
			model.addAttribute("trainingInfo", training1Mapper.selectByPrimaryKey(id));
			PageHelper.startPage(pageNum, 10);
			TrainingListExample example = new TrainingListExample();
			com.uestc.pojo.TrainingListExample.Criteria c = example.createCriteria();
			c.andParentIdEqualTo(id);
			example.setOrderByClause("create_time desc");
			List<TrainingList> list = trainingListMapper.selectByExample(example);
			PageInfo<TrainingList> pageInfo = new PageInfo<>(list);
			model.addAttribute("pageInfo", pageInfo);
			model.addAttribute("trainingList", list);
			return PATH + "pages/training_list";
		}
	}

	@GetMapping(value = "/delTraining/{parentId}/{id}")
	public String delTraining(Model model, @PathVariable(required = true) Integer parentId,
			@PathVariable(required = true) Integer id) {
		trainingListMapper.deleteByPrimaryKey(id);
		return "redirect:/admin/training1/" + parentId;
	}

	@GetMapping(value = { "/updatetraining/{id}" })
	public String updatetraining(Model model, @PathVariable Integer id) {
		model.addAttribute("training", trainingListMapper.selectByPrimaryKey(id));
		return PATH + "pages/updatetraining";
	}

	@PostMapping(value = { "/updateTraining/{id}" })
	public String updateParty(Model model, @PathVariable Integer id, NewsTypeTemplate newsTypeTemplate) {
		TrainingList part = new TrainingList();
		BeanUtils.copyProperties(newsTypeTemplate, part);
		part.setCreateTime(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		part.setId(id);
		trainingListMapper.updateByPrimaryKeySelective(part);
		return "redirect:/admin/updatetraining/" + id;
	}

	@GetMapping(value = { "/addtraining", })
	public String addtraining(Model model) {
		return PATH + "pages/addtraining";
	}

	@PostMapping(value = "/addTraining")
	public String addTraining(NewsTypeTemplate newsTypeTemplate, MultipartFile file) {
		TrainingList part = new TrainingList();
		BeanUtils.copyProperties(newsTypeTemplate, part);
		part.setCreateTime(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		trainingListMapper.insertSelective(part);
		TrainingListExample e1=new TrainingListExample();
		com.uestc.pojo.TrainingListExample.Criteria c1 = e1.createCriteria();
		c1.andCreateTimeEqualTo(DateUtil.StringToDate(newsTypeTemplate.getTime(), DateStyle.YYYY_MM_DD_HH_MM));
		c1.andTitleEqualTo(newsTypeTemplate.getTitle());
		c1.andSourceEqualTo(newsTypeTemplate.getSource());
		c1.andParentIdEqualTo(newsTypeTemplate.getParentId());
		List<TrainingList> trainingList = trainingListMapper.selectByExample(e1);
		if (trainingList.size() == 1) {
			if (file != null && !file.isEmpty()) {
				String fileUrl = HttpUtil.saveFile(file, host);
				Integer id = trainingList.get(0).getId();
				TrainingLink record = new TrainingLink();
				record.setParentId(id);
				// 加图标样式
				if (fileUrl.toLowerCase().contains(".zip") || fileUrl.toLowerCase().contains(".rar")) {
					record.setIconClass("fa fa-file-zip-o");
				} else if (fileUrl.toLowerCase().contains(".gif") || fileUrl.toLowerCase().contains(".jpg")
						|| fileUrl.toLowerCase().contains(".png")) {
					record.setIconClass("fa fa-file-image-o");
				} else if (fileUrl.toLowerCase().contains(".wav") || fileUrl.toLowerCase().contains(".mp3")) {
					record.setIconClass("fa fa-file-audio-o");
				} else if (fileUrl.toLowerCase().contains(".avi") || fileUrl.toLowerCase().contains(".mpg")) {
					record.setIconClass("fa fa-file-video-o");
				} else if (fileUrl.toLowerCase().contains(".doc") || fileUrl.toLowerCase().contains(".docx")) {
					record.setIconClass("fa fa-file-word-o");
				} else if (fileUrl.toLowerCase().contains(".ppt") || fileUrl.toLowerCase().contains(".pptx")) {
					record.setIconClass("fa fa-file-powerpoint-o");
				} else if (fileUrl.toLowerCase().contains(".pdf")) {
					record.setIconClass("fa fa-file-pdf-o");
				} else if (fileUrl.toLowerCase().contains(".xls") || fileUrl.toLowerCase().contains(".xlsx")) {
					record.setIconClass("fa fa-file-excel-o");
				} else {
					record.setIconClass("fa fa-folder-o");
				}
				record.setUrl(fileUrl);
				trainingLinkMapper.insertSelective(record);
			}
		}
		return "redirect:/admin/addtraining";
	}

	@PostMapping(value = "/saveTraining1/{id}")
	public String saveTraining1(Model model, @PathVariable(required = true) Integer id, Training1 record) {
		record.setId(id);
		training1Mapper.updateByPrimaryKeySelective(record);
		return "redirect:/admin/training1/" + id;
	}

	@GetMapping(value = "/login")
	public String login(Model model) {
		return PATH + "pages/login";
	}

	@PostMapping(value = "/doLogin")
	public String doLogin(Model model, User user, HttpServletRequest req) {
		UserExample example = new UserExample();
		com.uestc.pojo.UserExample.Criteria c = example.createCriteria();
		c.andUsernameEqualTo(user.getUsername());
		c.andPasswordEqualTo(user.getPassword());
		List<User> users = userMapper.selectByExample(example);
		if (!users.isEmpty() && users.size() == 1) {
			HttpSession session = req.getSession();
			session.setAttribute("user", users.get(0));
			return "redirect:/admin/index";
		} else {
			model.addAttribute("msg", "账号密码不匹配");
			return PATH + "pages/login";
		}
	}

	@GetMapping(value = "/uploadimgs")
	public String uploadimg(Model model) {
		SliderExample example1 = new SliderExample();
		com.uestc.pojo.SliderExample.Criteria c1 = example1.createCriteria();
		c1.andTypeEqualTo(2);
		model.addAttribute("sliderList", sliderMapper.selectByExample(example1));
		return PATH + "pages/uploadimgs";
	}

	@PostMapping(value = "/saveImage")
	public String saveImage(Model model, MultipartFile file, Integer type) {
		saveImage(file, type);
		return "redirect:/admin/uploadimgs";
	}

	@GetMapping(value = "/relatedlink")
	public String relatedlink(Model model) {
		RelatedLinkExample example = new RelatedLinkExample();
		List<RelatedLink> list = relatedLinkMapper.selectByExample(example);
		model.addAttribute("aboutUsDetails", list);
		return PATH + "pages/relatedlink";
	}

	@PostMapping(value = "/saveRelatedLink")
	public String saveRelatedLink(Model model, RelatedLink record) {
		relatedLinkMapper.insertSelective(record);
		return "redirect:/admin/relatedlink";
	}

	@PostMapping(value = "/updateRelatedLink/{id}")
	public String updateRelatedLink(Model model, @PathVariable Integer id, RelatedLink record) {
		record.setId(id);
		relatedLinkMapper.updateByPrimaryKeySelective(record);
		return "redirect:/admin/relatedlink";
	}

	@GetMapping(value = { "/delRelatedLink/{id}" })
	public String delRelatedLink(Model model, @PathVariable Integer id) {
		relatedLinkMapper.deleteByPrimaryKey(id);
		return "redirect:/admin/relatedlink";
	}

	@GetMapping(value = { "/test" })
	public String test() {
		return PATH + "pages/test";
	}
	
	@GetMapping(value = { "/research-platform/{id}", })
	public String researchPlatform(@PathVariable Long id, Model model,
			@RequestParam(name = "pageNum", defaultValue = "0") Integer pageNum) {
		model.addAttribute("categoryInfo", researchPlatformCategoryRepository.findById(id).get());
		model.addAttribute("categorieList", researchPlatformCategoryRepository.findAll());
		Pageable pageable = new PageRequest(pageNum, 10);
		org.springframework.data.domain.Page<ResearchPlatform> page = researchPlatformRepository.findByCategoryIdOrderByCreateTimeDesc(id, pageable);
		model.addAttribute("pageInfo", page);
		return PATH + "pages/research-platform";
	}
	
	@GetMapping(value = "/del-research-platform/{categoryId}/{id}")
	public String delResearchPlatform(Model model, @PathVariable(required = true) Long categoryId,
			@PathVariable(required = true) Long id) {
		researchPlatformRepository.deleteById(id);
		return "redirect:/admin/research-platform/" + categoryId;
	}
	
	@GetMapping(value = { "/add-research-platform", })
	public String addResearchPlatform(Model model) {
		model.addAttribute("categorieList", researchPlatformCategoryRepository.findAll());
		return PATH + "pages/add-research-platform";
	}
	

	@PostMapping(value = "/add-research-platform")
	@Transactional
	public String saveResearchPlatform(ResearchPlatform researchPlatform) {
		researchPlatformRepository.save(researchPlatform);
		return "redirect:/admin/add-research-platform";
	}
	

	@GetMapping(value = { "/update-research-platform/{id}" })
	public String updateResearchPlatform(Model model, @PathVariable Long id) {
		model.addAttribute("pageInfo", researchPlatformRepository.findById(id).get());
		model.addAttribute("categorieList", researchPlatformCategoryRepository.findAll());
		return PATH + "pages/update-research-platform";
	}

	@PostMapping(value = { "/update-research-platform/{id}" })
	public String updateResearchPlatform(Model model, @PathVariable Long id, ResearchPlatform researchPlatform) {
		researchPlatform.setId(id);
		researchPlatformRepository.saveAndFlush(researchPlatform);
		return "redirect:/admin/update-research-platform/" + id;
	}
}
